/**
 * @License: (C) Copyright 2013-2022
 * @Contact: abcque@outlook.com
 * @IDE: IntelliJ IDEA
 * @Encoding: utf-8
 * @Project: st2212 2213
 * @Package: PACKAGE_NAME
 * @File: YanghuiTriangle.java
 * @Time: 2022/12/16 8:09
 * @Author: abcque
 * @Description: 使用二维数组计算杨辉三角形
 * @Modifier:
 * @ModifyDate:
 * @ModifyRemark:
 */
public class YanghuiTriangle {
    public static void main(String[] args) {
        // step1、创建一个二维数组，名称为 triangle。行数 10 行。每行列数等于行数。
        int[][] triangle;
        triangle = new int[10][];

        for (int i = 0; i < triangle.length; i++) {
            triangle[i] = new int[i+1];
        }

        // step2、每行的第一个元素和最后一个元素的值都是 1
        for (int i = 0; i < triangle.length; i++) {
            triangle[i][0] = 1;
            triangle[i][triangle[i].length - 1] = 1;
        }

        // step3、给行中间的元素赋值
        for (int i = 2; i < triangle.length; i++) {
            for (int j = 1; j < triangle[i].length-1 ; j++) {
                triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1];
            }
        }

        // step4、输出二维数组所有的元素
        for (int i = 0; i < triangle.length; i++) {
            for (int j = 0; j < triangle[i].length; j++) {
                System.out.printf("%d ", triangle[i][j]);
            }
            System.out.println();
        }
    }
}
